﻿using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace DataObject.Order
{
    public class OrderDAL
    {
        public static DataTable GetListSearch(int customerId, int status, int pageIndex, int pageSize, ref int totalRecords)
        {
            var filter = "1=1";

            if (customerId > 0)
                filter += string.Format(" AND CustomerID = {0}", customerId);

            filter += status > 0
                          ? string.Format(" AND [Status] = {0}", status)
                          : string.Format(" AND [Status] IN ({0}, {1}, {2})",
                                (int)OrderStatus.OrderTemp, (int)OrderStatus.OrderComplete, (int)OrderStatus.OrderDeleted);

            totalRecords = Action.GetListPagingCount("[Order]", filter);
            return Action.DataTable_GetListPaging("[Order]", "OrderId DESC", Constants.Order_FieldList, filter, pageIndex, pageSize);
        }

        public static int Order_Create(OrderObject o)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Order_Create",
                                    o.CustomerId, o.Status, o.TotalValue, o.OrderDate, o.RequiredDate, o.Note);
        }

        public static void Order_Delete(int orderId)
        {
            SqlHelper.ExecuteScalar(Constants.ConnectionString, "Order_Delete", orderId);
        }
        
        public static void Order_Update_Status(int orderId, int status)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Order_Update_Status", orderId, status);
        }

        public static void Order_Update_Note(int orderId, string note)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Order_Update_Note", orderId, note);
        }        

        public static IDataReader Order_GetById(int orderId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Order_GetById", orderId);
        }
              
        public static int Order_GetStatistic(int status)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Order_GetStatistic", status);
        }
    }
}
